package cn.tqfeiyang.springplus.system.mapper;

import cn.tqfeiyang.springplus.system.entity.LogEntity;
import cn.tqfeiyang.springplus.system.entity.RoleEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户/角色关联接口
 *
 * @author tqfeiyang
 * @since 2024-07-30
 */
@Mapper
public interface UserRoleMapper {

    @Select("SELECT r.* FROM system_user_role ur JOIN system_role r ON r.role_id = ur.role_id AND r.deleted = 0 WHERE ur.user_id = #{userId}")
    List<RoleEntity> selectRoleListByUserId(Long userId);

    @Select("SELECT role_id FROM system_user_role WHERE user_id = #{userId}")
    List<Long> selectRoleIdsByUserId(Long userId);

    @Insert("<script>"
            + "INSERT INTO system_user_role(user_id, role_id) "
            + "SELECT #{userId}, role_id "
            + "FROM system_role "
            + "WHERE role_id IN "
            + "<foreach item='id' index='index' collection='roleIds' open='(' separator=',' close=')'>"
            + "#{id}"
            + "</foreach>"
            + "</script>")
    int insertRoleIdsByUserId(Long userId, List<Long> roleIds);

    @Delete("<script>"
            + "DELETE FROM system_user_role "
            + "WHERE user_id = #{userId} "
            + "AND role_id IN "
            + "<foreach item='id' index='index' collection='roleIds' open='(' separator=',' close=')'>"
            + "#{id}"
            + "</foreach>"
            + "</script>")
    int deleteRoleIdsByUserId(Long userId, List<Long> roleIds);

    @Delete("DELETE FROM system_user_role WHERE user_id = #{userId}")
    void deleteAllRoleIdsByUserId(Long userId);



//    @Select("SELECT user_id FROM system_user_role WHERE role_id = #{roleId}")
//    List<Long> selectUserIdsByRoleId(Long roleId);
//
//    @Delete("DELETE FROM system_user_role WHERE role_id = #{roleId}")
//    void deleteUserIdsByRoleId(Long roleId);
//
//    @Insert("INSERT INTO system_user_role(user_id, role_id) VALUES(#{userId}, #{roleId})")
//    void insert(Long userId, Long roleId);
}
